﻿using System.IO;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;

public class UserData
{
    private bool isGrid = true;
    private bool is8_StartSliceArg = false;
    private bool getColor_StartSliceArg = true;
    private bool setColor_StartSliceArg = true;

    public void SetUserData(int offset, bool valuea)
    { 
        string pathDirectory = Application.dataPath + "/" + VoxelCategories.USERDATA_PATH + "/";

        using (FileStream fs = new FileStream(pathDirectory + "userdata", FileMode.Open))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                bw.Seek(offset, SeekOrigin.Begin);
                bw.Write(valuea);
            }
        }
    }

    public bool GetUserData(int offset)
    {
        bool valuea = false;
        string pathDirectory = Application.dataPath + "/" + VoxelCategories.USERDATA_PATH + "/";
        using (FileStream fs = new FileStream(pathDirectory + "userdata", FileMode.Open))
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                //for (int i = 0; i < offset; i++)
                //{
                //    valuea = br.ReadBoolean();
                //}
                if (offset > 0)
                {
                    br.ReadBytes(offset);
                }
                valuea = br.ReadBoolean();
                //try
                //{

                //    byte[] readData = new byte[1];
                //    br.Read(readData, offset, 1);
                //    if (readData[0] == 1)
                //    {
                //        valuea = true;
                //    }
                //}
                //catch (System.Exception)
                //{

                //    int zzz = -0;
                //}
            }
        }
        return valuea;
    }

    static UserData thas;

    public static UserData Thas
    {
        get
        {
            if (thas == null)
            {
                thas = new UserData();
                string pathDirectory = Application.dataPath + "/" + VoxelCategories.USERDATA_PATH + "/";
                if (Directory.Exists(pathDirectory) == false)
                {
                    Directory.CreateDirectory(pathDirectory);
                }
                if (File.Exists(pathDirectory + "userdata") == false)
                {
                    using (FileStream fs = new FileStream(pathDirectory + "userdata", FileMode.Create))
                    {
                        using (BinaryWriter bw = new BinaryWriter(fs))
                        {
                            bw.Write(true);
                            bw.Write(false);
                            bw.Write(true);
                            bw.Write(true);
                        }
                    }
                }
            }
            return thas;
        }
    }

    public bool IsGrid
    {
        get
        {
            return GetUserData(0);
        }

        set
        {
            SetUserData(0, value);
        }
    }

    public bool Is8_StartSliceArg
    {
        get
        {
            return GetUserData(1);
        }

        set
        {
            SetUserData(1, value);
        }
    }

    public bool GetColor_StartSliceArg
    {
        get
        {
            return GetUserData(2);
        }

        set
        {
            SetUserData(2, value);
        }
    }

    public bool SetColor_StartSliceArg
    {
        get
        {
            return GetUserData(3);
        }

        set
        {
            SetUserData(3, value);
        }
    }
}
